﻿set ansi_padding, ansi_warnings, concat_null_yields_null, arithabort, quoted_identifier, ansi_nulls on
go

if exists (select * from tempdb..sysobjects where id=object_id('tempdb..#tmpProperties')) drop table #tmpProperties
go

create table #tmpProperties ( name nvarchar(255), value nvarchar(255) )
go

insert into #tmpProperties values('script', 'update.0004.sql')
insert into #tmpProperties values('error', '0')

set xact_abort on
go

set transaction isolation level serializable
go

begin transaction
go

set ansi_nulls on
set quoted_identifier on
go
/* BEGIN UPDATE SCRIPT */


/*
Add Call Center region to Administration Page
*/

-- Update script: Add Call reason, call topic, call category to Administration view
INSERT INTO [dbo].[DETAILVIEWS_RELATIONSHIPS]
           ([ID]
           ,[DELETED]
           ,[CREATED_BY]
           ,[DATE_ENTERED]
           ,[MODIFIED_USER_ID]
           ,[DATE_MODIFIED]
           ,[DATE_MODIFIED_UTC]
           ,[DETAIL_NAME]
           ,[MODULE_NAME]
           ,[CONTROL_NAME]
           ,[RELATIONSHIP_ORDER]
           ,[RELATIONSHIP_ENABLED]
           ,[TITLE])
     VALUES
           ('E8DE5A5E-3F7A-4916-B2B0-29775485EA2C'
           ,0
           ,null
           ,getdate()
           ,null
           ,getdate()
           ,getdate()
           ,'Administration.ListView'
           ,'Administration'
           ,'CallCenterView'
           ,12
           ,1
           ,'Administration.LBL_CALLCENTER_TITLE')
GO


INSERT INTO [dbo].[TERMINOLOGY]
           (ID
           ,[DELETED]
           ,[CREATED_BY]
           ,[DATE_ENTERED]
           ,[MODIFIED_USER_ID]
           ,[DATE_MODIFIED]
           ,[DATE_MODIFIED_UTC]
           ,[NAME]
           ,[LANG]
           ,[MODULE_NAME]
           ,[LIST_NAME]
           ,[LIST_ORDER]
           ,[DISPLAY_NAME])
     VALUES
           ('31545F76-5156-4015-AA4B-3B81207AEBCB'
           ,0
           ,null
           ,getdate()
           ,null
           ,getdate()
           ,getdate()
           ,'LBL_CALLCENTER_TITLE'
           ,'en-US'
           ,'Administration'
           ,null
           ,null
           ,'CALL CENTER')
GO

INSERT INTO [dbo].[TERMINOLOGY]
           ([ID]
           ,[DELETED]
           ,[CREATED_BY]
           ,[DATE_ENTERED]
           ,[MODIFIED_USER_ID]
           ,[DATE_MODIFIED]
           ,[DATE_MODIFIED_UTC]
           ,[NAME]
           ,[LANG]
           ,[MODULE_NAME]
           ,[LIST_NAME]
           ,[LIST_ORDER]
           ,[DISPLAY_NAME])
     VALUES
           ('3C2B7FE2-43C0-41B5-9C7B-C9A14276D716'
           ,0
           ,null
           ,getdate()
           ,null
           ,getdate()
           ,getdate()
           ,'LBL_CALLTOPIC'
           ,'en-US'
           ,'Administration'
           ,null
           ,null
           ,'Call Topic')
GO

INSERT INTO [dbo].[TERMINOLOGY]
           ([ID]
           ,[DELETED]
           ,[CREATED_BY]
           ,[DATE_ENTERED]
           ,[MODIFIED_USER_ID]
           ,[DATE_MODIFIED]
           ,[DATE_MODIFIED_UTC]
           ,[NAME]
           ,[LANG]
           ,[MODULE_NAME]
           ,[LIST_NAME]
           ,[LIST_ORDER]
           ,[DISPLAY_NAME])
     VALUES
           ('67C6451D-8047-4FEB-B308-DAE1C3CEB110'
           ,0
           ,null
           ,getdate()
           ,null
           ,getdate()
           ,getdate()
           ,'LBL_CALLTOPIC_CATEGORY'
           ,'en-US'
           ,'Administration'
           ,null
           ,null
           ,'Topic Category')
GO





/* END UPDATE SCRIPT */
go
if @@error<>0 and @@trancount>0 rollback transaction
go

if @@trancount=0 begin
	update #tmpProperties set value = 1 where name = 'error'
	begin transaction
end
go

if (select top 1 value from #tmpProperties where name = 'error') <> '0' rollback transaction
go

if @@trancount > 0 begin
	declare @@scriptName nvarchar(255)
	select top 1 @@scriptName = value from #tmpProperties where name = 'script'

	if dbo.scriptExisted(@@scriptName) = 1 begin
		rollback transaction
		print @@scriptName + ' already executes'
	end
	else begin
		exec dbo.updateScript @@scriptName
		commit transaction
		print 'The database update succeeded'
	end
END
else begin
	print 'The database update failed'
end
go

drop table #tmpProperties
go
